﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;

namespace TestSystem.Model.Data.Repositories
{
    public class ToolsRepository
    {
        private TestSystemEntities entities = new TestSystemEntities();

        public IQueryable<Tool> FindAll()
        {
            return entities.Tools.AsQueryable();
        }

        public Tool GetById(Int32 id)
        {
            return entities.Tools.SingleOrDefault(tool => tool.Id == id);
        }

        public void Insert(Tool tool)
        {
            entities.AddToTools(tool);
            
        }

        public void Delete(Tool tool)
        {
            entities.Tools.DeleteObject(tool);
        }

        public void Save()
        {
            entities.SaveChanges();
            
        }

        public void Dispose()
        {
            entities.Dispose();
        }

        public static void Rename(Tool tool, string newname, string description)
        {
            tool.Name = newname;
            tool.Description = description;
        }

        public IQueryable<Tool> FindInFolder(Folder folder)
        {
            return entities.Tools.Where(t => t.FolderId == folder.Id);
        }
    }
}